The problem can be found at the following link: Question Link
To reverse a doubly linked list, I go through the list, swapping the prev
and next
pointers for each node while updating the head during each iteration.
- Time Complexity:
O(N)
, whereN
is the number of nodes in the doubly linked list. - Auxiliary Space Complexity:
O(1)
, as the reversal is done in-place without using additional space.
class Solution {
public:
Node* reverseDLL(Node* head) {
Node* curr = head;
while (curr) {
head = curr;
Node* prev = curr->prev;
curr->prev = curr->next;
curr->next = prev;
curr = curr->prev;
}
return head;
}
};
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the getlost01/gfg-potd repository.